﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace VoiceCommand
{
    public class Json
    {
        /// <summary>
        /// 保存命令
        /// </summary>
        /// <param name="model"></param>
        public static void SaveVoiceCmd(List<Model.VoiceCmd> model)
        {
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(model);
            SaveJson("VoiceCmd", json);
        }

        /// <summary>
        /// 获取命令
        /// </summary>
        /// <returns></returns>
        public static List<Model.VoiceCmd> GetVoiceCmd()
        {
            var json = GetJson("VoiceCmd");
            var res = string.IsNullOrEmpty(json)
                ? new List<Model.VoiceCmd>() : Newtonsoft.Json.JsonConvert.DeserializeObject<List<Model.VoiceCmd>>(json);
            return res;
        }


        /// <summary>
        /// 保存JSON数据
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="json"></param>
        private static void SaveJson(string filename, string json)
        {
            try
            {
                var exeFolder = System.Windows.Forms.Application.StartupPath + "\\";
                var directory = exeFolder + "Json\\";
                var filepath = directory + "\\" + filename + ".json";
                if (!Directory.Exists(directory))
                    Directory.CreateDirectory(directory);

                File.WriteAllText(filepath, json);
            }
            catch (Exception e)
            {
                WriteTxt("保存JSON", $"{filename}.json 保存出错:\r\n{e.Message}");
            }
        }

        /// <summary>
        /// 获取JSON数据
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        private static string GetJson(string filename)
        {
            var res = "";
            try
            {
                var exeFolder = System.Windows.Forms.Application.StartupPath + "\\";
                var directory = exeFolder + "Json\\";
                var filepath = directory + "\\" + filename + ".json";
                // 创建一个 StreamReader 的实例来读取文件 
                // using 语句也能关闭 StreamReader
                using (var sr = new StreamReader(filepath))
                {
                    string line;

                    // 从文件读取并显示行，直到文件的末尾 
                    while ((line = sr.ReadLine()) != null)
                    {
                        res += line;
                    }
                }
            }
            catch (Exception e)
            {
                WriteTxt("获取JSON", $"{filename}.json 读取出错:\r\n{e.Message}");
            }
            return res;
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="folder">该日志所在文件夹名字</param>
        /// <param name="content"></param>
        public static void WriteTxt(string folder, string content)
        {
            try
            {
                var exeFolder = System.Windows.Forms.Application.StartupPath + "\\";
                var directory = exeFolder + "logs\\" + folder;
                var filepath = directory + "\\日志(" + DateTime.Now.ToString("yyyy-MM-dd") + ").txt";
                if (!Directory.Exists(directory))
                    Directory.CreateDirectory(directory);
                var sw = new StreamWriter(filepath, true, Encoding.UTF8);
                sw.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]:");
                sw.WriteLine("***************************分隔符【开始】***********************************");
                sw.WriteLine(content);
                sw.WriteLine("***************************分隔符【结束】***********************************\r\n");
                sw.Close();
            }
            catch
            {
                // ignored
            }
        }
    }
}
